package cn.wildfire.chat.app.extendsion.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.ArrayList;

import cn.wildfire.chat.app.extendsion.classes.Region;

/**
 * Create by HEKE, 2019/8/13 16:34
 * TODO 国际及区号适配器
 */
public class CountryCodesAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Region> regionArrayList;

    public CountryCodesAdapter(Context context, ArrayList<Region> regionArrayList) {
        this.mContext = context;
        this.regionArrayList = regionArrayList;
    }

    public void setRegionArrayList(ArrayList<Region> regionArrayList) {
        this.regionArrayList = regionArrayList;
    }

    @Override
    public int getCount() {
        return regionArrayList == null ? 0 : regionArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return regionArrayList == null ? null : regionArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        try {
            if (view == null) {
                view = View.inflate(mContext.getApplicationContext(), R.layout.country_code_item, null);
                holder = new ViewHolder();
                holder.textView = view.findViewById(R.id.countryCodeItem);
                holder.codeTextView = view.findViewById(R.id.countryCode);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            Region region = regionArrayList.get(i);
            holder.textView.setText("" + region.regionName);
            holder.codeTextView.setText("" + region.regionCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    private class ViewHolder {
        public TextView textView;
        public TextView codeTextView;
    }

}
